package com.cats.activity;

import android.content.Intent;
import android.location.Address;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;

import com.cats.R;

public class LocationActivity extends BaseActivity implements OnTabChangeListener, OnClickListener {

	private int findType = 0; //0: zipcode, 1: location
	private TabHost th;
	
	public LocationActivity() {
		// TODO Auto-generated constructor stub
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location);
		System.out.println("[LocationActivity]onCreate");
		setActionBarTitle("Location");
		th=(TabHost)findViewById(R.id.tabhost);
		th.setup();
		TabSpec specs=th.newTabSpec("tag1");
		specs.setContent(R.id.Zipcode);
		specs.setIndicator("Zipcode");
		th.addTab(specs);
		specs=th.newTabSpec("tag2");
		specs.setContent(R.id.MyLocation);
		specs.setIndicator("MyLocation ");
		th.addTab(specs);
		/**
		 * default tab: zip code
		 */
		th.setCurrentTab(0);
		
		th.setOnTabChangedListener(this);
		
		Button zipFindButton = (Button)findViewById(R.id.find);
		zipFindButton.setOnClickListener(this);
		Button locationFindButton = (Button)findViewById(R.id.find1);
		locationFindButton.setOnClickListener(this);
	}

	@Override
	public void onTabChanged(String tabId) {
		findType = th.getCurrentTab();
		System.out.println("[LocationActivity]onTabChanged to: " + findType);
	}

	/**
	 * click on find
	 */
	@Override
	public void onClick(View v) {
		if (findType == 0) {
			System.out.println("[LocationActivity]Find locations by zipcode");
			EditText editText = (EditText)findViewById(R.id.editText1);
//			String zipcodeInput = editText.getText().toString();
			Address zipcodeString = new Address(null);
			zipcodeString.setPostalCode(editText.getText().toString());
			
			/**
			 *  navigate to location map activity
			 */
			if (!(zipcodeString.getPostalCode().isEmpty()||zipcodeString.getPostalCode().length()>5)) {
				final Intent intent = new Intent(LocationActivity.this, LocationMapActivity.class);
				intent.putExtra("ZIPCODE", zipcodeString.getPostalCode().toString());
			    startActivity(intent);
			}
			else {
				this.showWarningWithMsg("Please input a valid zipcode.");
			}
		}
		else {
			System.out.println("[LocationActivity]Find locations by current location");
			final Intent intent = new Intent(LocationActivity.this, LocationMapActivity.class);
			// TODO: get current location info, set as parameter to location map activity
			//intent.putExtra("...", ...);
		    startActivity(intent);			
		}
	}
}



